package org.csu.group4.jpetstore.controller;

import org.csu.group4.jpetstore.mybatis.entity.Category;
import org.csu.group4.jpetstore.mybatis.entity.Orders;
import org.csu.group4.jpetstore.mybatis.entity.Product;
import org.csu.group4.jpetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class OrderController {
    @Autowired
    OrderService orderService;
    @GetMapping(value = "/order/orderlist")
    public ModelAndView getProducts(){
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getSession();
        String username = (String)session.getAttribute("localUser");
        ModelAndView modelAndView = new ModelAndView("order/ListOrders");
        List<Orders> orders = orderService.getAllByUsername(username);
        modelAndView.addObject("orders",orders);
        return modelAndView;
    }
    @GetMapping(value = "/order/neworder")
    public ModelAndView makenewOrder(){
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getSession();
        String username = (String)session.getAttribute("localUser");
        ModelAndView modelAndView = new ModelAndView("order/ConfirmOrder");
        List<Orders> orders = orderService.getAllByUsername(username);
        modelAndView.addObject("orders",orders);
        return modelAndView;
    }
    @GetMapping(value = "/order/vieworder")
    public ModelAndView viewnewOrder(){
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getSession();
        String username = (String)session.getAttribute("localUser");
        ModelAndView modelAndView = new ModelAndView("order/ViewOrder");
        List<Orders> orders = orderService.getAllByUsername(username);
        modelAndView.addObject("orders",orders);
        return modelAndView;
    }
}
